package com.controller;

import com.alibaba.fastjson.JSON;
import com.pojo.Admin;
import com.pojo.Member;
import com.pojo.Product;
import com.pojo.Result;
import com.server.impl.UserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @description：会员管理
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    UserServiceImpl userService;

    @RequestMapping(value ="/userlist",method = RequestMethod.GET)
    public String userlist(Model model){
        List<Member> user_list= userService.getUser();
        model.addAttribute("user_list",user_list);

        return "/pages/user_list" ;
    }

    @RequestMapping("/finduser")
    @ResponseBody
    public ModelAndView finduser(String username) {
        //1.调用service方法
        List<Member> user_list = userService.finduser(username);
        //2.实例化一个ModelAndView对象
        ModelAndView mv=new ModelAndView();
        mv.addObject("user_list",user_list);
        mv.setViewName("/pages/user_list");
        return mv;

    }
    /*@RequestMapping(value ="/insertUser",method = RequestMethod.GET)
    public String insertUser(int member_id,String member_name,String member_nickname,String member_gender,String member_phone,String member_email,Model model){
        userService.insertUser(member_id, member_name, member_nickname, member_gender, member_phone, member_email);
        return "/pages/user_list" ;
    }*/

    /*@RequestMapping(value ="/updateUser",method = RequestMethod.GET)
    public String updateUser(int member_id,String member_name,String member_nickname,String member_gender,String member_phone,String member_email,Model model){
        userService.updateUser(member_id, member_name, member_nickname, member_gender, member_phone, member_email);
        return "/pages/user_list" ;
    }*/
    /*@RequestMapping(value ="/edit",method = RequestMethod.GET)
    @ResponseBody
    public String updateUser(@Param("member_id") Integer member_id, Model model, HttpSession session){
        Result result = new Result();

        Member member1=userService.selectMemberById(member_id);
        System.out.println(member1);
        session.setAttribute("member1",member1);
        model.addAttribute("member1",member1);
        String jsonMember=JSON.toJSONString(member1);
        return jsonMember;

    }*/

    @RequestMapping(value ="/edit",method = RequestMethod.GET)
    @ResponseBody
    public Result updateUser(@Param("member_id") Integer member_id, Model model, HttpSession session){
        Result result = new Result();
        if (session.getAttribute("member1")!=null){
            session.removeAttribute("member1");
        }

        Member member1=userService.selectMemberById(member_id);
        System.out.println(member1);

        session.setAttribute("member1",member1);

        if (member1!=null){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);

        return result;

    }

    @RequestMapping(value ="/updateUser",method = RequestMethod.GET)
    @ResponseBody
    public Result updateUser(@Param("member_id") int member_id,@Param("member_name")String member_name,
        @Param("member_nickname")String member_nickname,@Param("member_gender")String member_gender,@Param("member_phone")String member_phone,
                             @Param("member_email")String member_email,HttpSession session){
        /*int member_id,String member_name,String member_nickname,String member_gender,String member_phone,String member_email*/
        Result result = new Result();

        int count=userService.updateUser(member_id, member_name, member_nickname, member_gender, member_phone, member_email);
        session.removeAttribute("member1");
        if (count>0){
            System.out.println("修改用户成功");
            result.setSuccess(true);
            return result;
        }
        System.out.println("修改用户失败");
        result.setSuccess(false);

        return result;
    }


    @RequestMapping(value ="/delUser",method = RequestMethod.GET)
    public String delUser(int member_id,Model model){
        userService.delUser(member_id);
        return "pages/user_list";
    }
}
